//
//  CusTabView.m
//  cusTabbarText
//
//  Created by xingshuai on 2020/9/28.
//

#import "CusTabView.h"

@interface CusTabView ()
{
    UIButton *_button;
    UIButton *_butTitle;
}

@end

@implementation CusTabView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self creatTabViewUI];
    }
    return self;
}

-(void)creatTabViewUI {
    NSArray *titleArrs = @[@"库克",@"老乔",@"工程师"];
    //  只需要该图片名字就行
    NSArray * normImage = @[@"tabbar_home",@"tabbar_home",@"tabbar_home"];
    //  只需修改选中图片的名字
    NSArray * selectImage = @[@"tabbar_homeSelect",@"tabbar_homeSelect",@"tabbar_homeSelect"];
    
    float btn_X = 0;
    float btn_width = self.frame.size.width/titleArrs.count;
    
    for(int i = 0;i<titleArrs.count;i++){
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(btn_X+btn_width*i, 0, btn_width, 34);
        [btn setImage:[UIImage imageNamed:normImage[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectImage[i]] forState:UIControlStateSelected];
        [self addSubview:btn];

        
        UIButton *titleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        titleBtn.frame = CGRectMake(btn_X+btn_width*i, 26, btn_width, 20);
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [titleBtn setTitleColor:[UIColor yellowColor] forState:(UIControlStateNormal)];
        [titleBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        [titleBtn setTitle:titleArrs[i] forState:(UIControlStateNormal)];
        [self addSubview:titleBtn];
        
        UIButton *chooseBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        chooseBtn.frame = CGRectMake(btn_X+btn_width*i, 0, btn_width, 54);
        [chooseBtn addTarget:self action:@selector(btnSelect:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:chooseBtn];
        
        if (i == 0) {
            _button = btn;
            _butTitle = titleBtn;
            titleBtn.selected = btn.selected = YES;
        }
        chooseBtn.tag = i;
        btn.tag = i+10;
        titleBtn.tag = i+20;
        
    }
}

-(void)btnSelect:(UIButton *)sender {
    
    _butTitle.selected = _button.selected = NO;

    UIButton *tmpImgBtn = [self viewWithTag:sender.tag+10];
    UIButton *tmpTitleBtn = [self viewWithTag:sender.tag+20];

    tmpTitleBtn.selected = tmpImgBtn.selected = YES;
    _butTitle = tmpTitleBtn;
    _button = tmpImgBtn;
    //self.selectedIndex = sender.tag;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickBtnTag:)]) {
        [self.delegate clickBtnTag:sender.tag];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
